<template>
  <div class="app-container">

    <div class="app-container__section" >
      <PlusSearch
        v-bind="getSearchBinds()"
        :columns="columns"
        v-model="searchForm"
      >
      </PlusSearch>
    </div>
    <div class="app-container__section">
      <PlusTable
        v-bind="getBinds()"
        :columns="columns"
      >
        <template #title>
          <table-title />
        </template>
        <template #toolbar>
          <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['message:notice:add']">新增</el-button>
          <el-button type="success" plain icon="Edit" :disabled="gSelection.length !== 1" @click="handleUpdate" v-hasPermi="['message:notice:edit']">修改</el-button>
          <el-button type="danger" plain icon="Delete" :disabled="gSelection.length === 0" @click="handleDelete" v-hasPermi="['message:notice:remove']">删除</el-button>
          <ClockTimer :task="gGetList" />
          <SearchAreaControl v-model="gShowSearchForm" />
        </template>
        <template #plus-cell-noticeType="scope">
          <dict-tag :options="sys_notice_type" :value="scope.row.noticeType" />
        </template>

        <template #plus-cell-status="scope">
          <dict-tag :options="sys_notice_status" :value="scope.row.status" />
        </template>
        <template #plus-cell-action="scope">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['message:notice:edit']">修改</el-button>
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['message:notice:remove']">删除</el-button>
        </template>
      </PlusTable>
    </div>

    <!-- 添加或修改公告对话框 -->
    <el-dialog :title="title" v-model="open" width="780px" append-to-body>
      <el-form ref="noticeRef" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="公告标题" prop="noticeTitle">
              <el-input v-model="form.noticeTitle" placeholder="请输入公告标题" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公告类型" prop="noticeType">
              <el-select v-model="form.noticeType" placeholder="请选择">
                <el-option v-for="dict in sys_notice_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="状态">
              <el-radio-group v-model="form.status">
                <el-radio v-for="dict in sys_notice_status" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="内容">
              <editor v-model="form.noticeContent" :min-height="192" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Notice">
import { listNotice, getNotice, delNotice, addNotice, updateNotice } from '@/api/system/notice.js'
import { useTablePage } from '@/hooks/useTablePage.js'
import { useActions } from '@/hooks/useActions.js'
import { PlusSearch, PlusTable } from 'plus-pro-components'

const { proxy } = getCurrentInstance()
const { sys_notice_status, sys_notice_type } = proxy.useDict('sys_notice_status', 'sys_notice_type')

// ===================================
const columns = computed(()=>[
  {
    label: '公告标题',
    prop: 'noticeTitle',
    tableColumnProps: {
      align: 'center'
    },
  },
  {
    label: '公告类型',
    prop: 'noticeType',
    tableColumnProps: {
      align: 'center'
    },
    valueType: 'select',
    options: sys_notice_type.value,
  },
  {
    label: '状态',
    prop: 'status',
    tableColumnProps: {
      align: 'center'
    },
    valueType: 'select',
    options: sys_notice_status.value,
  },
  {
    label: '创建者',
    prop: 'createBy',
    hideInSearch: true,
    tableColumnProps: {
      align: 'center'
    },
  },
  {
    label: '创建时间',
    prop: 'createTime',
    hideInSearch: true,
    tableColumnProps: {
      align: 'center'
    },
    valueType:'date-picker',
    fieldProps: {
      type: 'daterange',
      valueFormat: "YYYY-MM-DD"
    },
  },
  {
    label: '操作',
    prop: 'action',
    width: '160px',
    hideInSearch: true,
    tableColumnProps: {
      align: 'center',
      fixed:'right'
    }
  },
])

// 搜索条件
const searchForm = ref({})

const {
  gRouteMeta,
  gTableData,
  gTotal,
  gLoadingStatus,
  gPageInfo,
  gReset,
  gGetList,
  gOnPageChange,
  gSelection,
  gOnSelectionChange,
  gShowSearchForm,
  gResetForm,
  gResetPage,
  getBinds,
  getSearchBinds
} = useTablePage(listNotice, {
  searchForm,
})


const { deleteAction } = useActions();
function handleDelete(row) {
  deleteAction({
    api: delNotice,
    uniqueKey:'noticeId',
    selection: row.noticeId || gSelection.value
  }).then(() => {
    gGetList();
  });
}

// =============================================
// ===================================

const open = ref(false)
const title = ref('')

const data = reactive({
  form: {},
  rules: {
    noticeTitle: [{ required: true, message: '公告标题不能为空', trigger: 'blur' }],
    noticeType: [{ required: true, message: '公告类型不能为空', trigger: 'change' }]
  }
})

const { form, rules } = toRefs(data)

/** 取消按钮 */
function cancel() {
  open.value = false
  reset()
}
/** 表单重置 */
function reset() {
  form.value = {
    noticeId: undefined,
    noticeTitle: undefined,
    noticeType: undefined,
    noticeContent: undefined,
    status: '0'
  }
  proxy.resetForm('noticeRef')
}
/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = '添加公告'
}
/**修改按钮操作 */
function handleUpdate(row) {
  reset()
  const noticeId = row.noticeId || gSelection.value[0].noticeId
  getNotice(noticeId).then((response) => {
    form.value = response.data
    open.value = true
    title.value = '修改公告'
  })
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs['noticeRef'].validate((valid) => {
    if (valid) {
      if (form.value.noticeId != undefined) {
        updateNotice(form.value).then((response) => {
          proxy.$modal.msgSuccess('修改成功')
          open.value = false
          gGetList()
        })
      } else {
        addNotice(form.value).then((response) => {
          proxy.$modal.msgSuccess('新增成功')
          open.value = false
          gGetList()
        })
      }
    }
  })
}

</script>
